package tat.domain.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import tat.domain.AllClasses;
import tat.domain.TatClass;
import tat.exception.ObjectAlreadyExistsException;

public class AllClassesImpl implements AllClasses {

//	private TATAppLegacyWrapper appLegacyWrapper;

	private Map<String, TatClass> map = new HashMap<String, TatClass>();

    @Override
    public TatClass find(final String name) {
    	return map.get(name);
    }

	@Override
	public String classesAsText() {
		String text = "";
		Iterator<TatClass> classes = map.values().iterator();
		while (classes.hasNext()){
			text = text + classes.next().toString();
		}
		return text;
	}

	@Override
	public String classesAsXML() {
		String text = "";
		Iterator<TatClass> classes = map.values().iterator();
		while (classes.hasNext()){
			text = text + classes.next().toXML();
		}
		return text;
	}

	@Override
	public void registerClass(TatClass tatClass) throws ObjectAlreadyExistsException {
		if (map.containsKey(tatClass.name())) {
			throw new ObjectAlreadyExistsException("Already exists a class with name " + tatClass.name());
		}

		map.put(tatClass.name(), tatClass);
	}

	@Override
	public TatClass[] conferClasses() {
		return map.values().toArray(new TatClass[map.size()]);
	}

	@Override
	public void remove(String name) {
		map.remove(name);
	}

	@Override
	public void remapClass(String nomeAntigo, TatClass tatClass) {
		map.remove(nomeAntigo);
    	map.put(tatClass.name(), tatClass);
	}

}
